package view;

import domain.ATM;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import util.Calculator;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;



public class WithdrawalViewController implements ControlledStage, Initializable {

    private StageController myController;
    private ATM atm;
    @FXML
    private Button submit;
    @FXML
    private Button cancel;
    @FXML
    private TextField amount;
    @FXML
    private Button bt1;
    @FXML
    private Button bt2;
    @FXML
    private Button bt3;
    @FXML
    private Button bt4;
    @FXML
    private Button bt5;
    @FXML
    private Button bt6;
    @FXML
    private Button bt7;
    @FXML
    private Button bt8;
    @FXML
    private Button bt9;
    @FXML
    private Button bt0;
    @FXML
    private Button bt00;
    @FXML
    private Button bt100;
    @FXML
    private Button bt200;
    @FXML
    private Button bt500;
    @FXML
    private Button btback;
    @FXML
    private Button btclear;



    public void setStageController(StageController stageController) {
        this.myController = stageController;
        cancel.setOnAction(e -> myController.setStage(MainApp.mainViewID,MainApp.withdrawalViewID));
    }

    public void initialize(URL location, ResourceBundle resources) {
        bt0.setOnAction(e -> handle("0"));
        bt1.setOnAction(e -> handle("1"));
        bt2.setOnAction(e -> handle("2"));
        bt3.setOnAction(e -> handle("3"));
        bt4.setOnAction(e -> handle("4"));
        bt5.setOnAction(e -> handle("5"));
        bt6.setOnAction(e -> handle("6"));
        bt7.setOnAction(e -> handle("7"));
        bt8.setOnAction(e -> handle("8"));
        bt9.setOnAction(e -> handle("9"));
        bt00.setOnAction(e -> handle("00"));
        bt100.setOnAction(e -> handle("100"));
        bt200.setOnAction(e -> handle("200"));
        bt500.setOnAction(e -> handle("500"));
        btback.setOnAction(e -> handleBack());
        btclear.setOnAction(e -> handleClear());

    }


    public void goToMain(){
        myController.setStage(MainApp.mainViewID);
    }


    @FXML
    private void withdrawalAction(ActionEvent event) throws IOException {
        atm = myController.getAtm();
        System.out.println("You clicked the Button " + submit.getId());

        boolean isNegative = Calculator.conversion(amount.getText() + "-" + atm.getCurAccount().getBalance().getBalance()) > 0;
        boolean isOverLimit = Calculator.conversion(amount.getText() + "-" + ATM.MAX_AMOUNT_PER_WITHDRAWAL) > 0;

        if (amount.getText().endsWith("00")&&!isOverLimit&&!isNegative) {
            atm.enterAmount(amount.getText());
            atm.setCurMessageType("cwd");
            System.out.println(atm.getCurAccount().getBalance().getBalance());
        } else if(isOverLimit){
            atm.setCurMessageType("3");
        }
        else if(isNegative){
            atm.setCurMessageType("4");
        }
        else {
            atm.setCurMessageType("2");
        }
        myController.setAtm(atm);

        myController.loadStage(MainApp.messageViewID,MainApp.messageViewRes);
        myController.setStage(MainApp.messageViewID);

    }
    private void handleBack() {
        String text = amount.getText();
        int i = text.length();
        if (i > 0) {
            // 退格，将文本最后一个字符去掉
            text = text.substring(0, i - 1);
            amount.setText(text);

        }
    }

    private void handleClear() {
        amount.clear();
    }
    private void handle(String s) {
        amount.setText(amount.getText() + s);
    }
}
